package com.listeningframework.boot.codegen.context;

import com.listeningframework.boot.codegen.exception.CodeGenException;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by lixining on 2017/8/8.
 */
public class ConnectionContext {
    /**
     * 数据库类型
     */
    private String platform;
    /**
     * 数据库IP
     */
    private String host;
    /**
     * 数据库端口
     */
    private String port;
    /**
     * 数据库名称
     */
    private String database;
    /**
     * 数据库账号
     */
    private String username;
    /**
     * 数据库密码
     */
    private String password;

    public String getPlatform() {
        return platform;
    }

    public void setPlatform(String platform) {
        this.platform = platform;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isMySql() {
        return StringUtils.equalsIgnoreCase(platform, "mysql");
    }

    public String getURL() throws CodeGenException {
        if (isMySql()) {
            return "jdbc:mysql://" + host + ":" + port + "/" + database + "?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&autoReconnectForPools=true";
        } else {
            throw new CodeGenException("不支持的数据库类型");
        }
    }
}
